﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SuperX.Helper.Util
{
    public class EasyThread
    {
        private bool _abort;
        private readonly Thread _thread;

        private Action _action;
        private int _sleepTime;
        private int _stopTimeout;
        private Action<object> _action1;
        private object _parameter;
        private int _exeCount = 0;
        private int _defalutSleepTime = 500;

        public string Name { get; set; }

        public EasyThread(Action action, int sleepTime, int stopTimeout = 10)
        {
            _action = action;
            _sleepTime = sleepTime;
            _stopTimeout = stopTimeout;
            _thread = new Thread(Execute)
            {
                IsBackground = true
            };
        }

        public EasyThread(Action<object> action, object parameter, int sleepTime, int stopTimeout = 10)
        {
            _action1 = action;
            _parameter = parameter;
            _sleepTime = sleepTime;
            _stopTimeout = stopTimeout;
            _thread = new Thread(Execute)
            {
                IsBackground = true
            };
        }

        public void Start()
        {
            _abort = true;
            _thread.Start();
        }

        public void Stop()
        {
            _abort = false;
            var time = DateTime.Now;
            _parameter = null;
            while (true)
            {
                var now = DateTime.Now;
                if (_thread?.IsAlive == true)
                {
                    if ((now - time).TotalSeconds > _stopTimeout)
                    {
                        Abort();
                        return;
                    }
                    Thread.Sleep(20);
                }
                else return;
            }
        }

        public int GetThreadId()
        {
            return _thread.ManagedThreadId;
        }

        public void Abort()
        {
            _abort = false;
#if !NETSTANDARD2_0
            try
            {
                if (_thread.IsAlive)
                {
                    _thread.Abort();
                }
            }
            catch{}
#endif
        }

        private void Execute()
        {
            while (_abort && Thread.CurrentThread.ManagedThreadId == _thread?.ManagedThreadId)
            {
                try
                {
                    if (_exeCount == 0)
                    {
                        _action?.Invoke();
                        _action1?.Invoke(_parameter);
                        _exeCount = _sleepTime / _defalutSleepTime;
                        _exeCount = _exeCount < 0 ? 0 : _exeCount;
                    }

                    if (_sleepTime > 0 && _sleepTime <= 500) Thread.Sleep(_sleepTime);
                    else if (_sleepTime > 500)
                    {
                        Thread.Sleep(_defalutSleepTime);
                        _exeCount--;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
    }
}